package ken.app.even;

import java.util.ArrayList;
import java.util.List;

import ken.app.even.iface.IEventCallback;

import android.util.Log;

public class Event {
	
	Event startEvent;
	String mSignature = "";
	long mDelay = 0;
	Object mObj;
	public int timeout = 13000;
	public String account = null;
//	boolean mIsDiscardOnFailed = true;
	List<IEventCallback> mCallbacks = new ArrayList<IEventCallback>();
	protected List<Event> mSubEvents = new ArrayList<Event>();
	
	protected Event() {
		// 預設簽名為ClassName
		mSignature = this.getClass().getSimpleName();
	}
	
	@Override
	public boolean equals(Object o) {
		if(o instanceof Event) {
			Event event = (Event)o;
			if(event.getSignature().equals(mSignature)) return true;
		}
		return super.equals(o);
	}	
	
	public void setObj(Object object) {
		mObj = object;
	}
	
	public Object getObject() {
		return mObj;
	}
	
	public void setSignature(String signature) {
		mSignature = signature;
	}
	
	public String getSignature() {
		return mSignature;
	}
	
	public void setDelay(long milliseconds) {
		mDelay = milliseconds;
	}
	
	public void setSubEvents(List<Event> subEvents) {
		if(subEvents != null) mSubEvents = subEvents;
		else                  mSubEvents.clear();
	}
	
	public void addSubEvent(Event event) {
//		if(HamiMusic.isDebug) Log.e("@@@", event.getClass().getSimpleName() + " has " + event.getSubEvents().size() + "sub events");
		if(!mSubEvents.contains(event)) {
//			if(HamiMusic.isDebug) Log.e("@@", "contains sub event");
			mSubEvents.add(event);
		}
		else {
//			Log.e("@@", "do not contains sub event, insert " + event.getSubEvents().size() + " sub events");
////			mSubEvents.get(mSubEvents.indexOf(event)).addCallbacks(event.getCallbacks());
//			mSubEvents.get(mSubEvents.indexOf(event)).addSubEvents(event.getSubEvents());
		}
	}
	
	public void addSubEvents(List<Event> events) {
		for(int i = 0; i < events.size(); i++) {
			addSubEvent(events.get(i));
		}
	}
	
	public void setCallback(IEventCallback callback) {
		if(callback != null) {
			mCallbacks = new ArrayList<IEventCallback>(); 
			mCallbacks.add(callback);
		}
		else mCallbacks.clear();
	}
	
	public void setCallbacks(List<IEventCallback> callbacks) {
		if(callbacks != null) mCallbacks = callbacks;
		else                  mCallbacks.clear();
	}
	
	public void addCallback(IEventCallback callback) {
		mCallbacks.add(callback);
	}
	
	public void addCallbacks(List<IEventCallback> callbacks) {
		for(int i = 0; i < callbacks.size(); i++) {
			addCallback(callbacks.get(i));
		}
	}
	
	public long getDelay() {
		return mDelay;
	}
	
	// Only for read usage - Please call addSubEvent(s) for add
	public List<Event> getSubEvents() {
		return mSubEvents;
	}
	
	// Only for read usage - Please call addSubEvent(s) for add
	public List<IEventCallback> getCallbacks() {
		return mCallbacks;
	}
	
//	public boolean isDiscardOnFailed() {
//		return mIsDiscardOnFailed;
//	}
//	
//	public void setDiscardOnFailed(boolean isDiscardOnFailed) {
//		mIsDiscardOnFailed = isDiscardOnFailed;
//	}
}
